﻿using System.Linq;
using EveKillboard.Core;
using System;

namespace Doublons
{
    class Program
    {
        static void Main(string[] args)
        {
            var dap = new EveKillboard.Data.DataAccessProvider();
            var kills = dap
                .FindAll<Killmail>(k => k.Destroyed.Group.CategoryId == 23, false)
                .GroupBy(g => new { date = g.Date, location = g.LocationId, damage = g.Damage, item = g.DestroyedId, topDamage = g.Involved.First(i => i.IsTopDamage) })
                .Where(g => g.Count() > 1)
                .Select(g => new { key = g.Key, values = g.Select(k => k.Id).ToArray() })
                .ToArray();

            if (kills.Any())
                foreach (var doublon in kills)
                {
                    var current = doublon;
                    for (int i = 1; i < current.values.Length; i++)
                    {
                        Console.WriteLine("Deleting killmail Id:\"" + current.values[i] + "\"");
                        dap.SqlQuery("exec dbo.sp_DeleteKillmail {0}", new object[] { current.values[i] });
                    }
                }

            dap.Dispose();
            dap = null;
        }
    }
}
